<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-input v-model="listQuery.code" placeholder="字典code(请输入完整code)" class="filter-item" style="width: 210px;" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter" >
        查询
      </el-button> -->
      <el-button class="filter-item" type="primary" icon="el-icon-edit"
        @click="handleCreate">
        添加
      </el-button>
      <el-button class="filter-item" type="danger" icon="el-icon-delete" @click="handlePlDelete"
        >
        批量删除
      </el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row row-key="id" style="width: 100%;"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" />
      <el-table-column label="序号" type="index" align="center" width="60px" />
      <el-table-column label="姓名" prop="name" align="center" />
      <el-table-column label="手机号" prop="mobile" align="center" />
      <el-table-column label="车牌号" prop="carNo" align="center" />
      <el-table-column label="备注" prop="remark" align="center" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="140px">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)" >
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize"
      @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="88px"
        style="margin: 0 50px;">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="temp.name" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="temp.mobile" />
        </el-form-item>
        <el-form-item label="车牌号" prop="carNo">
          <el-input v-model="temp.carNo" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="temp.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="saveData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { driverPageByQo, driverSaveOrUpdate, driverRemoveByIds } from '@/api/company'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'Driver',
  directives: { waves },
  components: { Pagination },
  data() {
    const validateCity = (rule, value, callback) => {
      if (!this.temp.districtId) {
        callback(new Error('请选择省市区'))
      } else {
        callback()
      }
    }
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        pageSize: 20
      },
      temp: {
        id: 0,
        name: '',
        mobile: '',
        carNo: '',
        remark: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑司机',
        create: '添加司机'
      },
      rules: {
        name: [{ required: true, message: '请输入', trigger: 'change' }],
        mobile: [{ required: true, validator: this.$mobileValidator, trigger: 'blur' }],
        carNo: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
      multipleSelection: [],
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      driverPageByQo(this.listQuery).then(response => {
        this.list = response.list || []
        this.total = response.totalCount
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    resetTemp() {
      this.temp = {
        id: 0,
        name: '',
        mobile: '',
        carNo: '',
        remark: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    saveData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          delete this.temp.createTime
          driverSaveOrUpdate(this.temp, this.dialogStatus !== 'create').then(() => {
            this.getList()
            this.dialogFormVisible = false
            // this.$notify({
            //   title: '成功',
            //   message: '保存成功',
            //   type: 'success',
            //   duration: 2000
            // })

            this.$message({
              message: '保存成功',
              type: 'success'
            });
          })
        }
      })
    },
    // 批量删除
    handleSelectionChange(val) {
      this.multipleSelection = val.map((ele) => {
        return ele.id
      })
    },
    handlePlDelete() {
      if (!this.multipleSelection || this.multipleSelection.length < 1) {
        this.$message.error('请至少选择一条数据')
        return
      }
      const params = {
        ids: this.multipleSelection.join(',')
      }
      this.$confirm('是否继续删除？').then(() => {
        driverRemoveByIds(params).then(() => {
          // this.$notify({
          //   title: '成功',
          //   message: '批量删除成功！',
          //   type: 'success',
          //   duration: 2000
          // })
          this.$message({
            message: '批量删除成功！',
            type: 'success'
          });
          this.getList()
        })
      })
    }
  }
}
</script>
